package com.aim.analysis.records;

import com.aim.analysis.questionnaire.QuestionnaireRecord;
import com.aim.azure.AzureConnection;
import com.aim.azure.AzureDemographicEntity;
import com.aim.azure.AzureGameQuestionnaireEntity;
import com.aim.azure.AzureGameRecordEntity;
import com.aim.util.filters.InCollectionFilter;

import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.util.*;
import java.util.logging.Logger;

/**
 * User: Avraham Shvartzon
 * Date: 6/17/14
 */
public class Hit implements Comparable<Hit> {

    static Logger logger = Logger.getLogger(Hit.class.getCanonicalName());

    public String workerId;
    public String assignmentId;
    public List<Game> gameList;
    public QuestionnaireRecord questionnaireRecord;

    public Hit(String workerId, String assignmentId, List<Game> gameList, QuestionnaireRecord questionnaireRecord) {
        this.workerId = workerId;
        this.assignmentId = assignmentId;
        this.gameList = gameList;
        this.questionnaireRecord = questionnaireRecord;
    }

    public int totalScores(){
        int score = 0;
        for (Game game : gameList) {
            score += game.getFinalScore();
        }
        return score;
    }

    public Date getDate(){
        return gameList.get(0).getRecords(RecordType.gameStarted).get(0).date;
    }

    public static List<Hit> createHits(GameMap gameMap, AzureConnection azureConnection){
        List<AzureGameQuestionnaireEntity> gameQuestionsRecords = azureConnection.getGameQuestionsRecords();
        InCollectionFilter<String> filter = new InCollectionFilter<String>();

        for (AzureGameQuestionnaireEntity gameQuestionsRecord : gameQuestionsRecords) {
            filter.add(gameQuestionsRecord.getPartitionKey());
        }
        azureConnection.setAssignmentIdFilter(filter);

        List<AzureGameRecordEntity> gameRecords = azureConnection.getGameRecords();
        for (AzureGameRecordEntity gameRecordEntity : gameRecords) {
            GameRecord record = GameRecord.fromAzureRecord(gameRecordEntity);
            gameMap.addRecord(record);
        }
        gameMap.postProcessing();
        System.out.println("gameRecords.size() = " + gameRecords.size());
        List<Hit> hits = new ArrayList<Hit>();


        List<AzureDemographicEntity> demographicRecords = azureConnection.getDemographicRecords();
        Map<String, AzureDemographicEntity> demographicEntityMap = new HashMap<String, AzureDemographicEntity>();
        for (AzureDemographicEntity demographicRecord : demographicRecords) {
            demographicEntityMap.put(demographicRecord.getPartitionKey(), demographicRecord);
        }


        for (AzureGameQuestionnaireEntity gameQuestionsRecord : gameQuestionsRecords) {
            String assignmentId = gameQuestionsRecord.getPartitionKey();
            List<Game> games = gameMap.getByName(assignmentId);
            AzureDemographicEntity azureDemographicEntity = demographicEntityMap.get(assignmentId);
            QuestionnaireRecord questionnaireRecord = null;
            try {
                questionnaireRecord = QuestionnaireRecord.create(gameQuestionsRecord, azureDemographicEntity);
            } catch (Exception e) {

            }
            Hit hit = new Hit(questionnaireRecord.workerId, assignmentId, games, questionnaireRecord);
            if (hit.gameList != null && hit.gameList.size() >= 3){
            hits.add(hit);
            } else {
                logger.severe("missed games: " + hit.assignmentId);
            }
        }
        return hits;
    }

    public static List<Hit> getHits(String... experimentsDates){
        List<Hit> hits = null;
        AzureConnection azureConnection = new AzureConnection();
        try {
            azureConnection.connect();
            //azureConnection.addExperimentDate("20140616");
            for (String experimentsDate : experimentsDates) {
                azureConnection.addExperimentDate(experimentsDate);
            }
            GameMap gameMap = new GameMap();
            hits = createHits(gameMap, azureConnection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hits;
    }



    public static void main(String[] args) throws URISyntaxException, InvalidKeyException {
        AzureConnection azureConnection = new AzureConnection();
        azureConnection.connect();
        //azureConnection.addExperimentDate("20140616");
       azureConnection.addExperimentDate("Test");

        GameMap gameMap = new GameMap();
        List<Hit> hits = createHits(gameMap, azureConnection);
        System.out.println(hits.size());

    }

    @Override
    public int compareTo(Hit o) {
        return this.getDate().compareTo(o.getDate());
    }
}
